﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MaLu.Common;
using MaLu.BusiLogic;
using System.Text;
using MaLu.Data;
using System.Data;
using MaLu.Framework.Common;


namespace MaluJg.web.Three
{
    public partial class T2Edit : ThreePage
    {
        protected string CanAdd = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "添加走村入户";
            ScriptManager1.RegisterAsyncPostBackControl(lbtnSave);
            ScriptManager1.RegisterAsyncPostBackControl(btnAdd);
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            int status = (int)MaLu.Common.Const.EventStatus.已提交;
            btnAdd.Visible = false;
            bool hasRecord = false;
            if (EventID != Guid.Empty)
            {

                DataSet ds = BLThree.GetEvent2(Provider, EventID);
                if (ds.Tables.Count >= 2)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        Title = "编辑走村入户";
                        DataRow drInfo = ds.Tables[0].Rows[0];
                        status = int.Parse(drInfo["Status"].ToString());
                        tbDept.Text = drInfo["DeptDspName"].ToString();
                        tbSign.Text = drInfo["Signature"].ToString();
                        tbYear.Text = drInfo["year"].ToString();
                        tbMonth.Text = drInfo["month"].ToString();
                        tbDay.Text = drInfo["day"].ToString();
                        if (IsLeader)
                            Title = "审阅走村入户";

                        hasRecord = true;
                    }

                    DataTable dtIns = ds.Tables[1];
                    rpInstance.DataSource = dtIns;
                    rpInstance.DataBind();
                   
                }



            }
            if (hasRecord)
            {
                if (IsLeader)
                {
                    tbDept.ReadOnly = true;
                    tbYear.ReadOnly = true;
                    tbMonth.ReadOnly = true;
                    tbDay.ReadOnly = true;



                    lbtnSave.Text = "<span><img src=\"../Images/Icon/Object/File-add.gif\" /></span><span>审阅</span>";
                    btnAdd.Visible = false;

                }
                else
                {
                    tbSign.ReadOnly = true;
                    lbtnSave.Text = "<span><img src=\"../Images/Icon/Object/File-add.gif\" /></span><span>保存</span>";
                }
            }
            else
            {
                tbSign.ReadOnly = true;

            }
            if (status == (int)MaLu.Common.Const.EventStatus.已审阅)
            {
                CanAdd = "1";
                btnAdd.Visible = false;
                lbtnSave.Visible = false;

            }
            else
                btnAdd.Visible = true;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (tbID1.Text == string.Empty)
            {


                string sql = string.Format(@"INSERT INTO [TEvent2Instance]
           ([EventID] ,[Name],[IsMost] ,[IsHRChange],[IsArrage] ,[IsMoneyUse],[MakeTime],[IsAllPass]
           ,[IsMinorOBeyMajor],[IsDone],[IsIng],[IsUnDo] ,[Change] ,[Remark])  VALUES
           ('{0}',{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13});", EventID, Util.ReturnSqlValue(tbName.Text.Trim())
               , cblType.Items[0].Selected ? 1 : 0, cblType.Items[1].Selected ? 1 : 0, cblType.Items[2].Selected ? 1 : 0
             , cblType.Items[3].Selected ? 1 : 0, Util.ReturnSqlValue(tbMakeTime.Text.Trim())
                , cblDiscuss.Items[0].Selected ? 1 : 0, cblDiscuss.Items[1].Selected ? 1 : 0,
                cblExecute.Items[0].Selected ? 1 : 0, cblExecute.Items[1].Selected ? 1 : 0
                , cblExecute.Items[2].Selected ? 1 : 0, Util.ReturnSqlValue(tbChange.Text.Trim()), Util.ReturnSqlValue(tbRemark.Text.Trim()));


              SqlHelper.Instance.ExecuteNonQuery(Provider, sql);
            }
            else
            {
                string sql = string.Format(@"UPDATE [TEvent2Instance]
   SET [Name] ={0}
      ,[IsMost] = {1}
      ,[IsHRChange] = {2}
      ,[IsArrage] = {3}
      ,[IsMoneyUse] = {4}
      ,[MakeTime] = {5}
      ,[IsAllPass] ={6}
      ,[IsMinorOBeyMajor] = {7}
      ,[IsDone] = {8}
      ,[IsIng] ={9}
      ,[IsUnDo] ={10}
      ,[Change] ={11}
      ,[Remark] = {12}
 WHERE ID={13}",Util.ReturnSqlValue(tbName.Text.Trim())
               , cblType.Items[0].Selected ? 1 : 0, cblType.Items[1].Selected ? 1 : 0, cblType.Items[2].Selected ? 1 : 0
             , cblType.Items[3].Selected ? 1 : 0, Util.ReturnSqlValue(tbMakeTime.Text.Trim())
                , cblDiscuss.Items[0].Selected ? 1 : 0, cblDiscuss.Items[1].Selected ? 1 : 0,
                cblExecute.Items[0].Selected ? 1 : 0, cblExecute.Items[1].Selected ? 1 : 0
                , cblExecute.Items[2].Selected ? 1 : 0, Util.ReturnSqlValue(tbChange.Text.Trim()), Util.ReturnSqlValue(tbRemark.Text.Trim()) ,tbID1.Text);

               SqlHelper.Instance.ExecuteNonQuery(Provider, sql);
                btnAdd.Text = "<span ><img src=\"../Images/add.png\" alt=\"单条添加\" /></span><span>单条添加</span>";


            }
            BindDataDetail();

            tbName.Text = string.Empty;
            tbChange.Text = string.Empty;
            tbRemark.Text = string.Empty;
            tbMakeTime.Text = string.Empty;
            foreach (ListItem item in cblDiscuss.Items)
            {
                item.Selected = false;
            }
            foreach (ListItem item in cblExecute.Items)
            {
                item.Selected = false;
            }

            foreach (ListItem item in cblType.Items)
            {
                item.Selected = false;
            }

          
            tbID1.Text = string.Empty;
            UpdatePanel1.Update();

        }
        protected void rpInstance_ItemDataBound(object source, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex >= 0)
            {
                DataRowView drv = e.Item.DataItem as DataRowView;
                LinkButton lbtnDelete = e.Item.FindControl("lbtnDelete") as LinkButton;
                lbtnDelete.CommandArgument = drv["ID"].ToString();
                LinkButton lbtnEdit = e.Item.FindControl("lbtnEdit") as LinkButton;
                lbtnEdit.CommandArgument = drv["ID"].ToString();

            }
        }
        private void BindDataDetail()
        {
            DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, "select * from TEvent2Instance where EventID='" + EventID + "'");
         
            rpInstance.DataSource = dt;
            rpInstance.DataBind();
        }
        protected void rpInstance_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Delete":


                    SqlHelper.Instance.ExecuteNonQuery(Provider, "delete from TEvent2Instance where id='" + e.CommandArgument + "'");
                    BindDataDetail();

                    break;
                case "Edit":
                    DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, "select * from TEvent2Instance where id='" + e.CommandArgument + "'");
                    if (dt.Rows.Count > 0)
                    {
                        DataRow dr = dt.Rows[0];
                        tbID1.Text = dr["ID"].ToString();
                        tbName.Text = dr["Name"].ToString();
                        tbRemark.Text = dr["Remark"].ToString();
                        tbMakeTime.Text = dr["MakeTime"].ToString();
                        cblType.Items[0].Selected = dr["IsMost"].ToString() == "是";
                        cblType.Items[1].Selected = dr["IsHRChange"].ToString() == "是";
                        cblType.Items[2].Selected = dr["IsArrage"].ToString() == "是";
                        cblType.Items[3].Selected = dr["IsMoneyUse"].ToString() == "是";

                        cblDiscuss.Items[0].Selected = dr["IsAllPass"].ToString() == "是";
                        cblDiscuss.Items[1].Selected = dr["IsMinorOBeyMajor"].ToString() == "是";

                        cblExecute.Items[0].Selected = dr["IsDone"].ToString() == "是";
                        cblExecute.Items[1].Selected = dr["IsIng"].ToString() == "是";
                        cblExecute.Items[2].Selected = dr["IsUnDo"].ToString() == "是";

                        btnAdd.Text = "<span ><img src=\"../Images/Icon/Action/save.gif\" alt=\"保存\" /></span><span>保存</span>";
                        UpdatePanel1.Update();
                    }
                    break;
            }
        }
        
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            int result = 0;
            int status = (int)MaLu.Common.Const.EventStatus.已提交;
            string alert = "已提交！";
            if (IsLeader)
            {

                status = (int)MaLu.Common.Const.EventStatus.已审阅;
                alert = "已审阅！";
            }

            Guid gd = EventID == Guid.Empty ? Guid.NewGuid() : EventID;
            result = BLThree.SaveEvent2(Provider, gd, CurrentUser.MainDepartmentID, tbDept.Text, status,
               tbSign.Text, tbYear.Text, tbMonth.Text, tbDay.Text, CurrentUser.Name,DateTime.Parse( DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")), CurrentUser.Name);

            if (result == 1 && !IsLeader)
            {
                alert = "已审阅,不能再提交！";

            }

            System.Web.UI.ScriptManager.RegisterStartupScript(UpdatePanel3, this.GetType(), "save", "alert('" + alert + "!')", true);
            UpdatePanel3.Update();
        }

        protected Guid EventID
        {
            get
            {
                Guid gd = Guid.NewGuid();
                if (Request["ID"] != null)
                {
                   
                    gd = new Guid(Request["ID"] ?? Guid.NewGuid().ToString());
                }
                else
                {
                    if (ViewState["ID"] != null)
                        gd = new Guid( ViewState["ID"].ToString());


                }
                if (ViewState["ID"] == null)
                    ViewState["ID"] = gd;
                return gd;
            }
            set
            {
                ViewState["ID"] = value;
            }

        }
    }
}
